package org.springsocial.app;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

@EnableTransactionManagement
@PropertySource({"classpath:persistence.properties","classpath:social.properties"})
@Configuration
public class JpaConfiguration implements TransactionManagementConfigurer {
	private static final String PASSWORD_PROPERTY = "dataSource.password";
	private static final String USERNAME_PROPERTY = "dataSource.username";
	private static final String URL_PROPERTY = "dataSource.url";
	private static final String DRIVER_CLASS_NAME_PROPERTY = "dataSource.driverClassName";

	@Autowired
    private Environment env;
	
	

	@Bean
	public DataSource configureDataSource() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName(env.getProperty(DRIVER_CLASS_NAME_PROPERTY));
		dataSource.setUrl(env.getProperty(URL_PROPERTY));
		dataSource.setUsername(env.getProperty(USERNAME_PROPERTY));
		dataSource.setPassword(env.getProperty(PASSWORD_PROPERTY));
		//logger.info("----password:"+env.getProperty(PASSWORD_PROPERTY));
		System.out.println("----driver:"+env.getProperty(DRIVER_CLASS_NAME_PROPERTY));
		System.out.println("----url:"+env.getProperty(URL_PROPERTY));
		System.out.println("----username:"+env.getProperty(USERNAME_PROPERTY));
		System.out.println("----password:"+env.getProperty(PASSWORD_PROPERTY));
		return dataSource;
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() {
		LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
		entityManagerFactoryBean.setDataSource(configureDataSource());
		entityManagerFactoryBean.setPackagesToScan("org.springsocial");
		entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
		Properties jpaProperties = new Properties();
		jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, env.getProperty(org.hibernate.cfg.Environment.DIALECT));
		//jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, env.getProperty(org.hibernate.cfg.Environment.HBM2DDL_AUTO));
		jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, env.getProperty(org.hibernate.cfg.Environment.SHOW_SQL));
		entityManagerFactoryBean.setJpaProperties(jpaProperties);
		return entityManagerFactoryBean;
	}


	/*@Bean
	public PlatformTransactionManager transactionManager() {
		return new JpaTransactionManager(  );
	}*/
	
	@Bean
	public Twitter twitter() {
		TwitterTemplate template = new TwitterTemplate(env
				.getProperty("twitter.consumerKey"),env
				.getProperty("twitter.consumerSecret"),env
				.getProperty("twitter.accessToken"),env
				.getProperty("twitter.accessTokenSecret"));
		
		return template;
	}

	@Override
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		  JpaTransactionManager transactionManager = new JpaTransactionManager();
	      transactionManager.setEntityManagerFactory( 
	    		  configureEntityManagerFactory().getObject() );
	      
	      return transactionManager;
	}
	
}
